package com.debai.controller;

import com.debai.pojo.Admin;
import com.debai.service.AdminService;
import com.debai.util.JwtUtil;
import com.debai.util.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Log4j2
public class AdminController {
    @Autowired
    private AdminService adminService;

    /*
    admin用户登录
     */
    @PostMapping("/login")
    public Result login(@RequestParam(name = "adminName") String adminCount, @RequestParam(name = "adminPassword") String adminPassword) {
        Admin admin = adminService.selectByAccountAndPassword(adminCount, adminPassword);

        //用Map集合进行封装
        Map<String, Object> claims = new HashMap<>();
        if (admin != null) {
            claims.put("adminId", admin.getId());
            claims.put("adminName", admin.getAdminCount());
            //生成jwt令牌
            String jwt = JwtUtil.genToken(claims);
            System.out.println("登录成功！！！！！");
            return Result.success(jwt);
        } else {
            System.out.println("登录失败！！！");
            return Result.error("用户名或密码错误");
        }
    }



}
